﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HitchHikersGuideToTheGalaxy.Models
{
    public class VehicleSortRepository : IVehicleSortRepository
    {
        GalaxyDataContext db = new GalaxyDataContext();

        #region Default Add, Delete and save actions

        /// <summary>
        /// Add VehicleSort
        /// </summary>
        /// <param name="vehicleSort">Object of VehicleSort type.</param>
        public void Add(VehicleSort vehicleSort)
        {
            db.VehicleSorts.InsertOnSubmit(vehicleSort);
        }

        /// <summary>
        /// Delete VehicleSort
        /// </summary>
        /// <param name="vehicleSort">Object of VehicleSort type</param>
        public void Delete(VehicleSort vehicleSort)
        {
            db.VehicleSorts.DeleteOnSubmit(vehicleSort);
        }

        /// <summary>
        /// Save changes to the database.
        /// </summary>
        public void Save()
        {
            db.SubmitChanges();
        }

        #endregion

        #region Querys

        /// <summary>
        /// Get all VehicleSorts
        /// </summary>
        /// <returns>VehicleSort collection</returns>
        public IQueryable<VehicleSort> FindAllVehicleSorts()
        {
            return db.VehicleSorts;
        }

        /// <summary>
        /// Get Sort by ID
        /// </summary>
        /// <param name="id">Sort id</param>
        /// <returns>String sort</returns>
        public string GetSortById(int id)
        {
            return (from a in db.VehicleSorts
                    where a.Id == id
                    select a.Sort).SingleOrDefault();
        }

        /// <summary>
        /// Get Id by Sort
        /// </summary>
        /// <param name="sort">Sort name</param>
        /// <returns>ID of the sort</returns>
        public int GetIdBySort(string sort)
        {
            return (from a in db.VehicleSorts
                    where a.Sort == sort
                    select a.Id).SingleOrDefault();
        }

        #endregion

        
    }
}
